package system

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"private-domain-overseas-service/app/business/api/internal/svc"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/hzsystemconfig"
	"private-domain-overseas-service/tools/ent/predicate"
)

type SystemConfigDao struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSystemConfigDao(ctx context.Context, svcCtx *svc.ServiceContext) *SystemConfigDao {
	return &SystemConfigDao{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// GetKeyList - 获取配置列表
func (l *SystemConfigDao) GetKeyList(keyStr string) ([]*ent.HzSystemConfig, error) {
	valueList, err := l.svcCtx.Orm.HzSystemConfig.Query().
		Where(hzsystemconfig.SystemKeyEQ(keyStr)).
		All(l.ctx)
	if err != nil {
		return nil, err
	}
	return valueList, nil
}

// SystemConfigConditionsFirst - 根据条件获取配置列表
func (l *SystemConfigDao) SystemConfigConditionsFirst(conditions []predicate.HzSystemConfig) (*ent.HzSystemConfig, error) {
	systemConfig, err := l.svcCtx.Orm.HzSystemConfig.Query().
		Where(conditions...).
		First(l.ctx)
	if err != nil {
		return nil, err
	}
	return systemConfig, nil
}
